package com.fand.persistent.builder;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;

/**
 * 映射生成器工具
 * 
 * @author yaoHT
 */
public class ModelBuilderUtils {
	/**
	 * 实例化对象映射生成器
	 * 
	 * @param modelArgss
	 *            对象参数
	 * @return 对象映射生成器实例
	 */
	public static ModelBuilder newModelBuilder(final ModelArgs... modelArgss) {
		return ModelBuilderUtils.put(null, modelArgss);
	}
	
	/**
	 * 填充
	 * 
	 * @param modelBuilder
	 *            对象映射生成器
	 * @param modelArgss
	 *            对象参数
	 */
	public static ModelBuilder put(ModelBuilder modelBuilder, final ModelArgs... modelArgss) {
		if (modelBuilder == null) {
			modelBuilder = new DefaultModelBuilder();
		}
		
		if (org.apache.commons.lang3.ArrayUtils.isEmpty(modelArgss)) { return modelBuilder; }
		
		for (final ModelArgs modelArgs : modelArgss) {
			if (modelArgs == null) {
				continue;
			}
			
			if (ArrayUtils.isNotEmpty(modelArgs.getKeyBases())) {
				modelBuilder.putExtend(modelArgs.getKeyPrefix(), modelArgs.getKeyBases(),
				        modelArgs.getKeySuffix(), modelArgs.getValue());
			}
			
			if (MapUtils.isNotEmpty(modelArgs.getKvMap())) {
				modelBuilder.putExtend(modelArgs.getKeyPrefix(), modelArgs.getKvMap(), modelArgs.getKeySuffix());
			}
			
			if ((modelArgs.getO() != null) || (modelArgs.getC() != null)) {
				modelBuilder.putObjectField(modelArgs);
			}
		}
		
		return modelBuilder;
	}
}
